/*
 * VHController.java
 * Created on 28 mai 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.mvc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.LocaleInfo;
import com.hockeo.vhweb.client.mvc.VHEvent.EventType;
import com.hockeo.vhweb.client.rpc.VHBaseDataService;
import com.hockeo.vhweb.client.rpc.VHBaseDataServiceAsync;

/**
 * Base class for all VH controllers.
 *
 * @version $Id$
 * @author jjanke
 */
public abstract class VHController
{
  private static VHBaseDataServiceAsync d_serviceBaseData;
  private static final String           d_strLocale = LocaleInfo.getCurrentLocale().getLocaleName();
  private VHView                        d_view;

  /**
   * Creates a new controller.
   *
   * @param view the view to be associated to this controller.
   */
  protected VHController( VHView view )
  {
    d_view = view;
    d_view.setController( this );
  }

  /**
   * Returns the associated view.
   */
  protected VHView getView()
  {
    return d_view;
  }

  public static String getLocale()
  {
    return d_strLocale;
  }

  /**
   * Returns the service for asynchronous server calls with subject to all kind of
   * application base data.
   */
  public static VHBaseDataServiceAsync getBaseDataService()
  {
    if ( d_serviceBaseData == null )
      d_serviceBaseData = GWT.create( VHBaseDataService.class );

    return d_serviceBaseData;
  }

  /**
   * Handles any possibly incoming application level event. By default, only the
   * {@link EventType#APP_INIT} event is handled. If you want to react to other events,
   * please override this method and make a reference back to this method (super).
   *
   * @param event the event to be handled
   */
  public void handleEvent( VHEvent event )
  {
    if ( event.getType() == EventType.APP_INIT )
      d_view.init();
  }
}
